from sklearn import svm
from sklearn.model_selection import train_test_split
from sklearn import metrics
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.feature_selection import SelectKBest

sns.set()


def f_importances(X, Y):
    f, ax = plt.subplots(figsize=(10, 8))
    ax.set_title('Features Importance')
    sclt = SelectKBest(k='all')
    sclt.fit(X, Y)
    score = sclt.scores_
    features = X.columns
    namedScore = zip(features, score)
    sortedNamedScore = sorted(namedScore, key=lambda z: z[1], reverse=True)
    sortedScore = [each[1] for each in sortedNamedScore]
    sortedName = [each[0] for each in sortedNamedScore]
    y_pos = np.arange(len(features))
    plt.barh(y_pos, sortedScore, height=0.7, align='center', color='#AAAAAA', tick_label=sortedName)
    plt.xlabel('Feature Score')
    plt.ylabel('Feature Name')
    for score, pos in zip(sortedScore, y_pos):
        plt.text(score + 20, pos, '%.1f' % score, ha='center', va='bottom', fontsize=8)
    plt.show()


clf = svm.SVC(gamma='scale', kernel='linear')
data = pd.read_excel('test.xlsx')
X = data[data.columns.drop(['Y'])]
Y = data['Y']
x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.3)
clf.fit(x_train, y_train)
y_pre = clf.predict(x_test)
# acc = metrics.accuracy_score(y_pre,y_test)
# print(acc)

f_importances(X, Y)

crr_matrix = X.corr()
f, ax = plt.subplots(figsize=(10, 8))
sns.heatmap(crr_matrix, annot=True, cmap='YlGnBu')
ax.set_title('Features Correlation')
# f.savefig('features.png', dpi=100, bbox_inches='tight')
plt.show()
